package com.ninong.ker.admin.rest;

import java.sql.SQLException;
import java.util.List;

import com.ninong.ker.admin.service.dto.TenantDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ninong.ker.admin.sevice.TenantService;
import com.ninong.ker.common.annotation.log.Log;
import com.ninong.ker.common.annotation.perm.CmdAdmin;
import com.ninong.ker.common.response.CmdResponse;
import com.ninong.ker.common.tools.PropertyUtil;

/**
 * 系统租户
 * @author cmd
 *
 */
@RestController
@RequestMapping("/cmd/v1/tenant")
public class TenantResources {
	
	@Autowired
	private TenantService tenantService;

	
	@Log("租户列表")
	@CmdAdmin({"admin","tenant:list"})
	@PostMapping
	public ResponseEntity<CmdResponse> list(@RequestBody TenantDTO entity)  {
		CmdResponse response = new CmdResponse();
		response.setData(tenantService.listPage(entity));
		return ResponseEntity.ok(response);
	}
	
	@Log("租户删除")
	@CmdAdmin({"admin","tenant:delete"})
	@DeleteMapping
	public ResponseEntity<CmdResponse> delete(@RequestBody String[] ids)  {
		tenantService.delete(ids);
		CmdResponse response = new CmdResponse();
		String mess = PropertyUtil.getProperty("DELETE_SUCCESS");
		response.setMessage(mess);
		return ResponseEntity.ok(response);
	}
	
	@Log("租户新增或编辑")
	@CmdAdmin({"admin","tenant:add","tenant:update"})
	@PutMapping
	public ResponseEntity<CmdResponse> create(@RequestBody TenantDTO dto) throws Exception  {
		tenantService.create(dto);
		return ResponseEntity.ok(new CmdResponse());
	}
	
	@Log("租户详情")
	@GetMapping(value = "/{id}")
	public ResponseEntity<CmdResponse> get(@PathVariable String id)  {
		CmdResponse response = new CmdResponse();
		response.setData(tenantService.get(id));
		return ResponseEntity.ok(response);
	}

	@Log("租户菜单关系更新")
	@PostMapping(value = "/merge/{tenantId}")
	public ResponseEntity<CmdResponse> updateRoleMenuByRoleId(@PathVariable String tenantId,
															  @RequestBody List<String> menuIds) throws SQLException {
		tenantService.updateTenantMenuByTenantId(tenantId, menuIds);
		return ResponseEntity.ok(new CmdResponse());
	}

	@Log("获取已选中数据")
	@GetMapping(value = "/tree/{tenantId}")
	public ResponseEntity<CmdResponse> list(@PathVariable String tenantId) {
		CmdResponse response = new CmdResponse();
		response.setData(tenantService.listTreeByTenantId(tenantId));
		return ResponseEntity.ok(response);
	}
	
	
	@Log("激活禁用租户")
	@PostMapping(value = "/enable/{id}/{enable}")
	public ResponseEntity<CmdResponse> enableUser(@PathVariable String id,@PathVariable Boolean enable)  {
		CmdResponse response = new CmdResponse();
		tenantService.enableTenant(id,enable);
		return ResponseEntity.ok(response);
	}
	
	@Log("重置租户管理员登录密码")
	@PostMapping(value = "/resetPassword/{id}")
	public ResponseEntity<CmdResponse> resetPassword(@PathVariable String id)  {
		CmdResponse response = new CmdResponse();
		tenantService.resetPassword(id);
		return ResponseEntity.ok(response);
	}

}
